import data from "../../../../core/cache_data";
import Utils from "../../../../core/utils";
import AniPlayer from "../../../../prefabs/ani_player";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PageAniTest extends cc.Component {
    @property(AniPlayer)
    aniPlayer: AniPlayer = null;

    @property(cc.Node)
    histroyContain: cc.Node = null;
    @property(cc.EditBox)
    ebState: cc.EditBox = null;

    @property(cc.Node)
    histroyContain2: cc.Node = null;
    @property(cc.EditBox)
    eb: cc.EditBox = null;
    @property(cc.Label)
    lbLog: cc.Label = null;
    protected onLoad(): void {
        let list = data.storage.get("page_ani_test-histroy1", []);
        list.forEach(str => {
            let node = this._createHistroy(str);
            this.histroyContain.addChild(node);
            node.on(cc.Node.EventType.TOUCH_END, () => {
                this.playState(str);
            });
        });
        let list2 = data.storage.get("page_ani_test-histroy2", []);
        list2.forEach(str => {
            let node = this._createHistroy(str);
            this.histroyContain2.addChild(node);
            node.on(cc.Node.EventType.TOUCH_END, () => {
                this.play(str);
            });
        });
        this.node.on(cc.Node.EventType.TOUCH_MOVE, (evt: cc.Event.EventTouch) => {
            let delta = evt.getDelta();
            this.aniPlayer.node.x += delta.x;
            this.aniPlayer.node.y += delta.y;
        });
    }
    onClickPlay() {
        let aniName = this.ebState.string;
        this.playState(aniName);
    }
    onClickPlayAdditive() {
        let aniName = this.eb.string;
        this.play(aniName);
    }

    addHistroy(str: string, isState: boolean) {
        let node = this._createHistroy(str);
        if (isState) {
            this.histroyContain.addChild(node);
            node.on(cc.Node.EventType.TOUCH_END, () => {
                this.playState(str);
            });
        }
        else {
            this.histroyContain2.addChild(node);
            node.on(cc.Node.EventType.TOUCH_END, () => {
                this.play(str);
            });
        }
    }
    protected _createHistroy(str: string) {
        let node = new cc.Node(str);
        let lb = node.addComponent(cc.Label);
        lb.string = str;
        return node;
    }

    playState(aniName: string) {
        if (!aniName) {
            return;
        }
        this.aniPlayer.playState(aniName);
        this.addHistroy(aniName, true);
        let list = data.storage.get("page_ani_test-histroy1", []);
        list.push(aniName);
        data.save();

        this.log(`(${aniName})播放State`);
    }
    async play(aniName: string) {
        if (!aniName) {
            return;
        }
        let as = await this.aniPlayer.play(aniName, cc.WrapMode.Normal, true);
        this.addHistroy(aniName, false);
        let list = data.storage.get("page_ani_test-histroy2", []);
        list.push(aniName);
        data.save();
        this.log(`(${aniName})<${as.uid}>播放`);
        as.syncer.async().then(() => {
            this.log(`(${aniName})<${as.uid}>完成播放`);
        });
        as.syncer.subject.on("ani-msg", (msg: string) => {
            this.log(`(${aniName})<${as.uid}>动画事件：${msg}`);
        }, this);
    }
    log(str: string) {
        this.lbLog.string += Utils.timestampToTime(Date.now(), "m:s") + "   ";
        this.lbLog.string += str;
        this.lbLog.string += "\n";
    }
    onClickCleanLog() {
        this.lbLog.string = "";
    }
}
